/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package montecarlo.gui;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

import montecarlo.algorithms.*;
import montecarlo.control.ControlMC;

/**
 * 
 * @author Lizeth
 */
@SuppressWarnings("serial")
public class InterfaceMC extends javax.swing.JFrame {

	private boolean isSingleThread;
	private ControlMC control;
	private String[] tableValues;
	private AbstractClassMC monteC;
	private int poolSize;

	/**
	 * Creates new form InterfaceMC
	 */
	public InterfaceMC() {
		this.control = new ControlMC();
		this.tableValues = null;
		this.isSingleThread=true;
		initComponents();
	}

	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		listPanel = new javax.swing.JPanel();
		listOfSets = new java.awt.List();
		loadDataButton = new javax.swing.JButton();
		dataPanel = new javax.swing.JPanel();
		dataTable = new javax.swing.JScrollPane();
		valuesTable = new javax.swing.JTable();
		runButton = new javax.swing.JButton();
		infoPanel = new javax.swing.JPanel();
		resultLab = new javax.swing.JLabel();
		timeLab = new javax.swing.JLabel();
		time = new javax.swing.JLabel();
		result = new javax.swing.JLabel();
		infoMultiThreadPanel = new javax.swing.JPanel();
		coreLabel = new javax.swing.JLabel();
		coresNumber = new javax.swing.JLabel();
		poolNumber = new javax.swing.JLabel();
		poolLbl = new javax.swing.JLabel();
		bockingCoeffic = new javax.swing.JLabel();
		blockingCLbl = new javax.swing.JLabel();
		moreLbl = new javax.swing.JLabel();
		moreInfo = new javax.swing.JLabel();
		headerPanel = new java.awt.Panel();
		titleLabel = new javax.swing.JLabel();
		threadingPanel = new javax.swing.JPanel();
		singleThreadRB = new javax.swing.JRadioButton();
		multiThreadRB = new javax.swing.JRadioButton();
		footerPanel = new java.awt.Panel();
		myNameLb = new javax.swing.JLabel();
		groupLb = new javax.swing.JLabel();
		logoIcon = new javax.swing.JLabel();
		develLb = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setPreferredSize(new java.awt.Dimension(800, 550));
		setResizable(false);
		getContentPane().setLayout(null);

		listPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
				javax.swing.BorderFactory.createTitledBorder(""), "List",
				javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION, null,
				new java.awt.Color(0, 102, 102)));
		listPanel.setPreferredSize(new java.awt.Dimension(200, 600));
		listPanel.setLayout(null);
		listPanel.add(listOfSets);

		listOfSets.setBounds(10, 20, 240, 190);
		listOfSets.add("Set 1");
		listOfSets.add("Set 2");
		listOfSets.add("Set 3");
		listOfSets.add("Set 4");
		listOfSets.add("Set 5");
		listOfSets.add("Set 6");
		listOfSets.add("Set 7");
		listOfSets.add("Set 8");
		listOfSets.add("Set 9");
		listOfSets.add("Set 10");

		loadDataButton.setText("Load Data");
		loadDataButton.setPreferredSize(new java.awt.Dimension(80, 23));
		loadDataButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				loadDataButtonActionPerformed(evt);
			}
		});
		listPanel.add(loadDataButton);
		loadDataButton.setBounds(150, 230, 100, 23);

		getContentPane().add(listPanel);
		listPanel.setBounds(10, 160, 260, 270);

		dataPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
				"Contening",
				javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION, null,
				new java.awt.Color(0, 102, 102)));
		dataPanel.setPreferredSize(new java.awt.Dimension(520, 300));
		dataPanel.setLayout(null);

		valuesTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { null, null, null, null, null, null, null,
						null } }, new String[] { "S", "X", "r", "T", "b", "v",
						"nSteps", "nSimulations" }) {
			Class[] types = new Class[] { java.lang.String.class,
					java.lang.String.class, java.lang.String.class,
					java.lang.String.class, java.lang.String.class,
					java.lang.String.class, java.lang.String.class,
					java.lang.String.class };

			public Class getColumnClass(int columnIndex) {
				return types[columnIndex];
			}
		});
		valuesTable.setName(""); // NOI18N
		valuesTable.setPreferredSize(new java.awt.Dimension(515, 64));
		dataTable.setViewportView(valuesTable);

		dataPanel.add(dataTable);
		dataTable.setBounds(10, 20, 490, 100);

		runButton.setText("Run MonteCarlo");
		runButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				runButtonActionPerformed(evt);
			}
		});
		dataPanel.add(runButton);
		runButton.setBounds(353, 130, 140, 23);

		getContentPane().add(dataPanel);
		dataPanel.setBounds(280, 90, 510, 160);

		infoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
				"Information ",
				javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION, null,
				new java.awt.Color(0, 102, 102)));
		infoPanel.setLayout(null);

		resultLab.setText("Result:");
		resultLab.setPreferredSize(new java.awt.Dimension(40, 15));
		infoPanel.add(resultLab);
		resultLab.setBounds(20, 30, 200, 15);

		timeLab.setText("Time:");
		timeLab.setPreferredSize(new java.awt.Dimension(40, 15));
		infoPanel.add(timeLab);
		timeLab.setBounds(20, 100, 180, 15);

		time.setBackground(new java.awt.Color(255, 255, 255));
		time.setText("0");
		time.setPreferredSize(new java.awt.Dimension(40, 15));
		infoPanel.add(time);
		time.setBounds(60, 120, 120, 30);

		result.setBackground(new java.awt.Color(255, 255, 255));
		result.setText("0");
		result.setPreferredSize(new java.awt.Dimension(40, 15));
		infoPanel.add(result);
		result.setBounds(60, 50, 120, 30);

		infoMultiThreadPanel.setLayout(null);

		coreLabel.setText("Core:");
		infoMultiThreadPanel.add(coreLabel);
		coreLabel.setBounds(10, 10, 80, 14);

		coresNumber.setText("0");
		infoMultiThreadPanel.add(coresNumber);
		coresNumber.setBounds(140, 10, 90, 14);

		poolNumber.setText("0");
		infoMultiThreadPanel.add(poolNumber);
		poolNumber.setBounds(140, 30, 90, 14);

		poolLbl.setText("Pool Size/Threads:");
		infoMultiThreadPanel.add(poolLbl);
		poolLbl.setBounds(10, 30, 130, 14);

		bockingCoeffic.setText("0");
		infoMultiThreadPanel.add(bockingCoeffic);
		bockingCoeffic.setBounds(140, 50, 90, 14);

		blockingCLbl.setText("Blocking coefficient:");
		infoMultiThreadPanel.add(blockingCLbl);
		blockingCLbl.setBounds(10, 50, 120, 14);

		infoMultiThreadPanel.add(moreLbl);

		moreLbl.setBounds(10, 70, 120, 0);
		infoMultiThreadPanel.add(moreInfo);
		moreInfo.setBounds(140, 70, 90, 0);

		infoPanel.add(infoMultiThreadPanel);
		infoMultiThreadPanel.setBounds(200, 20, 300, 150);

		getContentPane().add(infoPanel);
		infoPanel.setBounds(280, 250, 510, 180);
		infoPanel.getAccessibleContext().setAccessibleDescription("");

		headerPanel.setBackground(new java.awt.Color(0, 102, 102));
		headerPanel.setLayout(null);

		titleLabel.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
		titleLabel.setForeground(new java.awt.Color(204, 204, 204));
		titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		titleLabel.setText("Monte Carlo");
		headerPanel.add(titleLabel);
		titleLabel.setBounds(10, 10, 780, 50);

		getContentPane().add(headerPanel);
		headerPanel.setBounds(0, 0, 800, 80);

		threadingPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
				null, "Threading",
				javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION, null,
				new java.awt.Color(0, 102, 102)));
		threadingPanel.setLayout(null);

		singleThreadRB.setSelected(true);
		singleThreadRB.setText("Single Thread");
		singleThreadRB.setPreferredSize(new java.awt.Dimension(90, 23));
		singleThreadRB.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				singleThreadRBActionPerformed(evt);
			}
		});
		threadingPanel.add(singleThreadRB);
		singleThreadRB.setBounds(10, 20, 120, 23);

		multiThreadRB.setText("Multi-Thread");
		multiThreadRB.setPreferredSize(new java.awt.Dimension(90, 23));
		multiThreadRB.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				multiThreadRBActionPerformed(evt);
			}
		});
		threadingPanel.add(multiThreadRB);
		multiThreadRB.setBounds(140, 20, 110, 23);

		getContentPane().add(threadingPanel);
		threadingPanel.setBounds(10, 90, 260, 70);

		footerPanel.setBackground(new java.awt.Color(51, 51, 51));
		footerPanel.setLayout(null);

		myNameLb.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		myNameLb.setForeground(new java.awt.Color(204, 204, 204));
		myNameLb.setText("Ronan GUYOMARD & Lizeth OLARTE");
		footerPanel.add(myNameLb);
		myNameLb.setBounds(300, 40, 300, 20);

		groupLb.setForeground(new java.awt.Color(204, 204, 204));
		groupLb.setText("ING4 - SII1");
		footerPanel.add(groupLb);
		groupLb.setBounds(710, 30, 80, 20);

		logoIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/montecarlo/gui/logoECE.png"))); // NOI18N
		logoIcon.setToolTipText("");
		footerPanel.add(logoIcon);
		logoIcon.setBounds(30, 0, 180, 80);

		develLb.setForeground(new java.awt.Color(204, 204, 204));
		develLb.setText("Developed by: ");
		footerPanel.add(develLb);
		develLb.setBounds(370, 20, 90, 20);

		getContentPane().add(footerPanel);
		footerPanel.setBounds(0, 440, 800, 80);

		// show visible of the panel for multi-threading false and the number of
		// cores
		infoMultiThreadPanel.setVisible(false);
		coresNumber.setText("" + control.getCores());
		bockingCoeffic.setText("" + control.getBlockingCoefficient());
		poolSize=control.getPoolSize();
		poolNumber.setText("" + poolSize);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	/**
	 * This method load one set of data from file ".txt" and display in the
	 * table
	 * 
	 * @param evt
	 */
	@SuppressWarnings("serial")
	private void loadDataButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_loadDataButtonActionPerformed
		try {
			tableValues = control.getData(listOfSets.getSelectedIndex());
			valuesTable.setModel(new javax.swing.table.DefaultTableModel(
					new Object[][] { { tableValues[1], tableValues[2],
							tableValues[3], tableValues[4], tableValues[5],
							tableValues[6], tableValues[7], tableValues[8] } },
					new String[] { "S", "X", "r", "T", "b", "v", "nSteps",
							"nSimulations" }) {
				Class[] types = new Class[] { java.lang.String.class,
						java.lang.String.class, java.lang.String.class,
						java.lang.String.class, java.lang.String.class,
						java.lang.String.class, java.lang.String.class,
						java.lang.String.class };

				public Class getColumnClass(int columnIndex) {
					return types[columnIndex];
				}
			});
		} catch (NullPointerException ex) {
			JOptionPane.showMessageDialog(this,
					"Please select any element of the list set.",
					"Select set item", JOptionPane.ERROR_MESSAGE);
		} catch (IOException ex) {
			Logger.getLogger(InterfaceMC.class.getName()).log(Level.SEVERE,
					null, ex);
		}
	}
	/**
	 * This method is for change the radio buttom from single to multi.
	 * 
	 * @param evt
	 */
	private void multiThreadRBActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_multiThreadRBActionPerformed
		infoMultiThreadPanel.setVisible(true);
		isSingleThread = false;
		multiThreadRB.setSelected(true);
		singleThreadRB.setSelected(false);

	}// GEN-LAST:event_multiThreadRBActionPerformed

	/**
	 * This method is for change the radio buttom from multi to single.
	 * 
	 * @param evt
	 */
	private void singleThreadRBActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_singleThreadRBActionPerformed
		infoMultiThreadPanel.setVisible(false);
		isSingleThread = true;
		singleThreadRB.setSelected(true);
		multiThreadRB.setSelected(false);
	}// GEN-LAST:event_singleThreadRBActionPerformed

	/**
	 * This method runs the Montecarlo Algorithm
	 * 
	 * @param evt
	 */
	private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_runButtonActionPerformed
		try {
			if(isSingleThread){
				monteC= new Single();
			}else{
				monteC = new Multi();
				monteC.setPoolSize(this.poolSize);
			}
			monteC.timeAndComputeValue(tableValues);
			time.setText("" + monteC.getExecutionTime());
			result.setText("" + monteC.getResult());
		}catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this,
					"Please choose a set of arguments", "Wait",
					JOptionPane.ERROR_MESSAGE);

		}catch (ExecutionException e ) {
			JOptionPane.showMessageDialog(this,
					"Seems has ocurred an execution Expeption. Please try again.", "Wait",
					JOptionPane.ERROR_MESSAGE);

		} catch (InterruptedException e) {
			JOptionPane.showMessageDialog(this,
					"The operation has been interrupeted", "Wait",
					JOptionPane.ERROR_MESSAGE);

		} catch (NullPointerException e) {
			JOptionPane.showMessageDialog(this,
					"Please choose a set of arguments", "Wait",
					JOptionPane.ERROR_MESSAGE);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(this,
					"Exception "+e.getMessage()+" "+e.getLocalizedMessage(), "Virgen del agarradero",
					JOptionPane.ERROR_MESSAGE);
		}

	}// GEN-LAST:event_runButtonActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(InterfaceMC.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(InterfaceMC.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(InterfaceMC.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(InterfaceMC.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new InterfaceMC().setVisible(true);
			}
		});
	}

	// Variables declaration for view//GEN-BEGIN:variables
	private javax.swing.JLabel blockingCLbl;
	private javax.swing.JLabel bockingCoeffic;
	private javax.swing.JLabel coreLabel;
	private javax.swing.JLabel coresNumber;
	private javax.swing.JPanel dataPanel;
	private javax.swing.JScrollPane dataTable;
	private javax.swing.JLabel develLb;
	private java.awt.Panel footerPanel;
	private javax.swing.JLabel groupLb;
	private java.awt.Panel headerPanel;
	private javax.swing.JPanel infoMultiThreadPanel;
	private javax.swing.JPanel infoPanel;
	private java.awt.List listOfSets;
	private javax.swing.JPanel listPanel;
	private javax.swing.JButton loadDataButton;
	private javax.swing.JLabel logoIcon;
	private javax.swing.JLabel moreInfo;
	private javax.swing.JLabel moreLbl;
	private javax.swing.JRadioButton multiThreadRB;
	private javax.swing.JLabel myNameLb;
	private javax.swing.JLabel poolLbl;
	private javax.swing.JLabel poolNumber;
	private javax.swing.JLabel result;
	private javax.swing.JLabel resultLab;
	private javax.swing.JButton runButton;
	private javax.swing.JRadioButton singleThreadRB;
	private javax.swing.JPanel threadingPanel;
	private javax.swing.JLabel time;
	private javax.swing.JLabel timeLab;
	private javax.swing.JLabel titleLabel;
	private javax.swing.JTable valuesTable;
	// End of variables declaration//GEN-END:variables
}
